<?php
/**
 * 图片分类控制器
 * @author silenus
 *
 */
class m_pcategory extends spModel {
	public $pk = 'pcategory_id';
	public $table = 'pcategory';
	/**
	 * 与图片是一对多关系
	 * 与自身是一对多关系
	 */
	var $linker = array (/**
	 * 与数据
	 */
	// 			array (
	// 					'type' => 'hasmany', // 多对多关联
	// 					'map' => 'article', // 关联的标识
	// 					'mapkey' => 'pcategory_id', // 关联的字段
	// 					'fclass' => 'm_data', // 对应表的数据类
	// 					'fkey' => 'pcategory_id', // 对应表的关联字段
	// // 					'field' => 'article_id,author,title' ,
	// 					'sort' => 'post_time DESC',
	// 					'enabled' => true 
	// 			),
	

	/**
	 * 与自身,父分类
	 */
	array ('type' => 'hasone', 'map' => 'parent', 'mapkey' => 'parent_id', 'fclass' => 'm_pcategory', 'fkey' => 'pcategory_id', 'field' => 'name,pcategory_id', 'enabled' => true ), 

	/**
	 * 与自身,子分类
	 */
	array ('type' => 'hasmany', 'map' => 'children', 'mapkey' => 'pcategory_id', 'fclass' => 'm_pcategory', 'fkey' => 'parent_id', 'field' => 'name,pcategory_id', // pcategory_id 看是否删除
// 					'sort' => 'sort_order',
	enabled => true ) );
	
	var $verifier = array ('rules' => array ('pcategory_name' => array (), 'parent_id' => array (), 'sort_order' => array (), 'code' => array () ) );
	
	public function tree($parent_id = 0, $map = array()) {
		$rst = $this->spLinker ()->findAll ( array ('parent_id' => $parent_id ) );
		if (empty ( $map )) {
			return $rst;
		} else {
			return $this->_tree_change_key ( $rst, $map );
		}
	}
	
	public function create_alburm($info_pcategory) {
		$this->create ( $info_pcategory );
	}
	public function mod_edit($pcategory_id, $content) {
		$this->update ( "`pcategory` = {$pcategory_id}", $content );
	}
	/**
	 *
	 * @param array $arrayList
	 * @param array $map 数组数据与返回数据之间的映射 改变键值
	 */
	protected function _tree_change_key($arrayList, $map = array()) {
		$return_arr = array ();
		foreach ( $arrayList as $List_key => $List_value ) {
			foreach ( $map as $map_key => $map_value ) {
				if (is_string ( $map_key ) && array_key_exists ( $map_key, $List_value )) {
					$return_arr [$List_key] [$map_value] = $List_value [$map_key];
				} else if (is_numeric ( $map_key )) {
					$return_arr [$List_key] [$map_value] = $List_value [$map_value];
				} else {
					return false;
				}
			}
		}
		return $return_arr;
	}
}